package org.gdstash.db;

import org.gdstash.util.GDMsgFormatter;
import org.gdstash.util.GDMsgLogger;

import java.sql.*;
import java.util.LinkedList;
import java.util.List;

public class DBEnginePlayerMasteries {
    public static final String TABLE_NAME = "GDC_PLAYER_MASTERIES";
    private static final int ROW_SKILLTREE_ID = 1;
    private static final int ROW_SKILLTREE_INDEX = 2;
    private String skillTreeID;
    private int index;

    public DBEnginePlayerMasteries() {
    }

    public DBEnginePlayerMasteries(final String skillTreeID, final int index) {
        this.skillTreeID = skillTreeID;
        this.index = index;
    }

    public String getSkillTreeID() {
        return this.skillTreeID;
    }

    public int getIndex() {
        return this.index;
    }

    public static boolean containsSkillTreeID(final List<DBEnginePlayerMasteries> list, final String id) {
        boolean found = false;
        for (final DBEnginePlayerMasteries mt : list) {
            if (mt.skillTreeID == null) {
                continue;
            }
            if (mt.skillTreeID.equals(id)) {
                found = true;
                break;
            }
        }
        return found;
    }

    public static DBEnginePlayerMasteries retrieveID(final List<DBEnginePlayerMasteries> list, final String id) {
        DBEnginePlayerMasteries epm = null;
        for (final DBEnginePlayerMasteries mt : list) {
            if (mt.skillTreeID == null) {
                continue;
            }
            if (mt.skillTreeID.equals(id)) {
                epm = mt;
                break;
            }
        }
        return epm;
    }

    public static void createTable(final Connection conn) throws SQLException {
        final String dropTable = "DROP TABLE GDC_PLAYER_MASTERIES";
        final String createTable = "CREATE TABLE GDC_PLAYER_MASTERIES (SKILLTREE_ID    VARCHAR(256) NOT NULL, SKILLTREE_INDEX INTEGER NOT NULL, PRIMARY KEY (SKILLTREE_ID))";
        try (final Statement st = conn.createStatement()) {
            if (GDDBUtil.tableExists(conn, "GDC_PLAYER_MASTERIES")) {
                st.execute(dropTable);
            }
            st.execute(createTable);
            st.close();
            conn.commit();
        } catch (SQLException ex) {
            throw ex;
        }
    }

    public static void delete(final Connection conn) throws SQLException {
        final String deleteEntry = "DELETE FROM GDC_PLAYER_MASTERIES";
        try (final PreparedStatement ps = conn.prepareStatement(deleteEntry)) {
            ps.executeUpdate();
            ps.close();
        } catch (SQLException ex) {
            throw ex;
        }
    }

    public static void insert(final Connection conn, final DBEnginePlayer dbPlayer) throws SQLException {
        if (dbPlayer.getMasteryTreeList() == null) {
            return;
        }
        if (dbPlayer.getMasteryTreeList().isEmpty()) {
            return;
        }
        final String insert = "INSERT INTO GDC_PLAYER_MASTERIES VALUES (?,?)";
        try (final PreparedStatement ps = conn.prepareStatement(insert)) {
            for (final DBEnginePlayerMasteries mt : dbPlayer.getMasteryTreeList()) {
                ps.setString(1, mt.skillTreeID);
                ps.setInt(2, mt.index);
                ps.executeUpdate();
                ps.clearParameters();
            }
            ps.close();
            conn.commit();
        } catch (SQLException ex) {
            throw ex;
        }
    }

    public static List<DBEnginePlayerMasteries> get() {
        List<DBEnginePlayerMasteries> list = null;
        final String command = "SELECT * FROM GDC_PLAYER_MASTERIES";
        try (final Connection conn = GDDBData.getConnection();
             final PreparedStatement ps = conn.prepareStatement(command)) {
            try (final ResultSet rs = ps.executeQuery()) {
                list = wrap(rs);
                conn.commit();
            } catch (SQLException ex) {
                throw ex;
            }
        } catch (SQLException ex2) {
            final Object[] args = {"-", "GDC_PLAYER_MASTERIES"};
            final String msg = GDMsgFormatter.format(GDMsgFormatter.rbMsg, "ERR_READ_TABLE_BY_ID", args);
            GDMsgLogger.addError(msg);
            GDMsgLogger.addError(ex2);
        }
        return list;
    }

    private static List<DBEnginePlayerMasteries> wrap(final ResultSet rs) throws SQLException {
        final LinkedList<DBEnginePlayerMasteries> list = new LinkedList<DBEnginePlayerMasteries>();
        while (rs.next()) {
            final DBEnginePlayerMasteries mt = new DBEnginePlayerMasteries();
            mt.skillTreeID = rs.getString(1);
            mt.index = rs.getInt(2);
            list.add(mt);
        }
        return list;
    }
}
